package com.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.server.common.mockData.WaterEntireNMockData;
import com.server.entity.DeviceInfo;
import com.server.mapper.DeviceInfoMapper;
import com.server.mapper.WaterEntireNMapper;
import com.server.entity.WaterEntireN;
import com.server.service.WaterEntireNService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.server.utils.GlobalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 总氨量(WaterEntireN)表服务实现类
 *
 * @author wzw
 * @since 2025-01-01 19:51:39
 */
@Service
public class WaterEntireNServiceImpl extends ServiceImpl<WaterEntireNMapper, WaterEntireN> implements WaterEntireNService {
    @Autowired
    DeviceInfoMapper deviceInfoMapper;
    @Autowired
    WaterEntireNMapper waterEntireNMapper;

    @Transactional
    public void updateWaterEntireN() throws ParseException {
        // 获取当前时间
        Date currentTime = GlobalUtils.transformStringToDate(GlobalUtils.getCurrentTime());
        currentTime=GlobalUtils.aroundDateToFiveMinute(currentTime);
        //获取随机日期
        Date randomTime =GlobalUtils.randomDate("2023-01-01 00:00:00","2024-07-21 00:00:00");
        randomTime=GlobalUtils.aroundDateToFiveMinute(randomTime);
        // 遍历每个设备
        QueryWrapper<WaterEntireN> qw;
        WaterEntireN rt;
        List<DeviceInfo> deviceList;
        Double newData = null;
        //获取随机合法deviceList
        QueryWrapper<DeviceInfo> deviceWrapper=new QueryWrapper<>();
        deviceWrapper.eq("functions", "water_entire_n").orderByAsc("stcd");
        deviceList=deviceInfoMapper.selectList(deviceWrapper);
        for(DeviceInfo di:deviceList) {
            int id = di.getId();
            Integer status = di.getStatus();
            // 查询表中是否存在设备的小时记录信息
            qw = new QueryWrapper<WaterEntireN>();
            qw.eq("time",randomTime);
            rt = waterEntireNMapper.selectOne(qw);
            // 如果当前数据不存在，则进行新增
            if (rt == null) {
                // 获取测试模拟映射数据
                Map<String, Double> MaxMinMap = (Map<String, Double>) WaterEntireNMockData.Map.get("range");
                int decimal = WaterEntireNMockData.Decimal;
                // 如果设备不正常或者模拟数据为极值为空，则不获取随机值
                if ( status==1 && MaxMinMap.get("max") != null && MaxMinMap.get("min") != null) {
                    newData = GlobalUtils.getRandomDataByRange(MaxMinMap.get("min"), MaxMinMap.get("max"), decimal);
                }
                // 插入模拟数据
                // System.out.println(id+","+currentTime+","+newData);
                // 新增数据
                rt = new WaterEntireN();
                rt.setTime(currentTime);
                rt.setDeviceId(id);//下一行需手动修改
                //rt.setWaterEntireN(newData);
                waterEntireNMapper.insert(rt);
            }
        }
    }

    @Override
    public List<WaterEntireN> findWaterEntireN(String start,String end) {
        QueryWrapper<WaterEntireN> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("time", start, end);
        queryWrapper.orderByDesc("time");
        return waterEntireNMapper.selectList(queryWrapper);
    }

    @Override
    public List<WaterEntireN> selectLatest() {
        // 获取当前时间,分钟个位数为0或5，向下取整
        Date currentTime=GlobalUtils.aroundDateToFiveMinute(GlobalUtils.transformStringToDate(GlobalUtils.getCurrentTime()));
        QueryWrapper<WaterEntireN> queryWrapper = new QueryWrapper<>();
        //queryWrapper.eq("time", currentTime);
        queryWrapper.orderByDesc("time").last("LIMIT 100");
        return waterEntireNMapper.selectList(queryWrapper);
    }
}
